package com.lsh.oms.provider.service.query;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.query.QuerySignTmsDto;
import com.lsh.oms.api.service.query.IFindOrderByMisService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.enums.InterfaceEnum;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderRoDetail;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.query.MisOrderHeadForDetail;
import com.lsh.oms.core.model.query.MisOrderHeadForList;
import com.lsh.oms.core.records.core.ReturnRepository;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.core.service.query.FindOrderByMisService;
import com.lsh.oms.core.service.shipping.core.ShippingService;
import com.lsh.oms.core.service.tool.ToolService;
import com.lsh.oms.provider.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;

import static com.lsh.oms.core.enums.InterfaceEnum.DELIVERY_DETAIL;

/**
 * @author panxudong
 * @date 16/8/19
 */
@Service(protocol = "rest")
@Path("order/find/query")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class FindOrderByMisRestServiceImpl extends BaseService implements IFindOrderByMisService {
    private static Logger logger = LoggerFactory.getLogger(FindOrderByMisRestServiceImpl.class);

    @Autowired
    private FindOrderByMisService findOrderByMisService;

    @Autowired
    private RoOrderService roOrderService;

    @Resource
    private ShippingService shippingService;

    @Resource
    private ReturnRepository returnRepository;

    @GET
    @Path("getOrderHeadList")
    @Override
    public String getOrderHeadList(@QueryParam("orderCodes") String orderCodes) {
        return this.getOrders(orderCodes, InterfaceEnum.ORDER_LIST);
    }

    @GET
    @Path("getReturnOrderList")
    @Override
    public String getReturnOrderList(@QueryParam("returnIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RETURN_LIST);
    }

    @GET
    @Path("getDeliveryOrderList")
    @Override
    public String getDeliveryOrderList(@QueryParam("shippingIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.DELIVERY_LIST);
    }

    @GET
    @Path("getReceiptOrderList")
    @Override
    public String getReceiptOrderList(@QueryParam("receiptIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RECEIPT_LIST);
    }

    @GET
    @Path("getOrderHead")
    @Override
    public String getOrderHead(@QueryParam("orderCode") Long orderCode) {
        return this.getOrder(orderCode, InterfaceEnum.ORDER_DETAIL);
    }

    @GET
    @Path("getReturnOrder")
    @Override
    public String getReturnOrder(@QueryParam("return_order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.RETURN_DETAIL);
    }

    @GET
    @Path("getAfsReturnOrder")
    @Override
    public String getAfsReturnOrder(@QueryParam("order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.AFTER_SALE_RO_ORDER);
    }

    @GET
    @Path("getDeliveryOrder")
    @Override
    public String getDeliveryOrder(@QueryParam("shipping_order_id") Long code) {
        return this.getOrder(code, DELIVERY_DETAIL);
    }

    @GET
    @Path("getReceiptOrder")
    @Override
    public String getReceiptOrder(@QueryParam("receipt_order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.RECEIPT_DETAIL);
    }

    @GET
    @Path("getReceiptOrder2")
    @Override
    public String getReceiptOrder2(@QueryParam("receipt_order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.RECEIPT_DETAIL_2);
    }

    @GET
    @Path("getOrderConfig")
    @Override
    public String getOrderConfig() {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();

        JSONObject configObject = new JSONObject();
        JSONObject orderStatusObject = new JSONObject(true);
        for (OrderStatus orderStatus : OrderStatus.values()) {
            orderStatusObject.put(String.valueOf(orderStatus.getIndex()), orderStatus.getName());
        }
        configObject.put("orderStatus", orderStatusObject);

        commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
        commonResult.setMessage("success.");
        commonResult.setData(configObject);
        return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero);
    }

    @GET
    @Path("getOrderListByAddressAndUser")
    @Override
    public String getOrderHeadListByAddressCodeAndUserCode(@QueryParam("addressCode") Long addressCode, @QueryParam("userCode") Long userCode) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (addressCode == null || userCode == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            List<OrderHead> orderHeadList = findOrderByMisService.getOrderHeadListByAddressCodeAndUserCode(addressCode, userCode);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(orderHeadList);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getOrderCountByAddress")
    @Override
    public String getOrderHeadCountByAddressCode(@QueryParam("addressCode") Long addressCode) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (addressCode == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            Integer count = findOrderByMisService.getOrderHeadCountByAddressCode(addressCode);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(count);
            return JSON.toJSONString(commonResult,
                    ToolService.valueFilter,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getInfoByAddressId")
    @Override
    public String getNewShippingInfoByAddressCode(@QueryParam("addressId") Long addressId) {
        logger.info("getNewShippingInfoByAddressCode logSign : " + addressId);
        CommonResult commonResult = new CommonResult();
        if (addressId == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {

            Optional<OrderShippingHeadWithBLOBs> shippingHeadWithBLOBsOptional = this.shippingService.findNewByAddressId(addressId,true);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(shippingHeadWithBLOBsOptional.get());
            return JSON.toJSONString(commonResult,
                    ToolService.valueFilter,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getOrderCodesByTimeRange")
    @Override
    public String getOrderCodesByTimeRange(@QueryParam("startTime") Integer startTime, @QueryParam("endTime") Integer endTime) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        logger.info("Request param : {} -- {}", startTime, endTime);
        CommonResult commonResult = new CommonResult();
        if (startTime == null || endTime == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            List<Long> orderCodes = findOrderByMisService.getOrderCodesByTimeRange(startTime, endTime);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(orderCodes);
            return JSON.toJSONString(commonResult);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    /**
     * 查询当前用户未支付的订单号
     *
     * @param uid
     * @return
     */
    @GET
    @Path("getNotPayOrderCodesByUid")
    @Override
    public String getNotPayOrderCodesByUid(@QueryParam("uid") Long uid) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (uid == null || uid.longValue() == 0L) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            List<Long> orderCodes = findOrderByMisService.getNotPayOrderCodesByUid(uid);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(orderCodes);
            return JSON.toJSONString(commonResult);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getReturnDetailsByReturnIds")
    @Override
    public String getReturnDetailsByReturnIds(@QueryParam("returnIds") String returnIds) {

        String logSign = ToolService.getLogSign();
        logger.info("getReturnDetailsByReturnIds Request logSign:" + logSign + ", Request Url:" + ToolService.getUrl() + "，" + returnIds);
        CommonResult commonResult = new CommonResult();
        if (StringUtils.isBlank(returnIds)) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            JSONArray returnCodeArray = JSONArray.parseArray(returnIds);
            List<Long> codes = new ArrayList<>();
            for (int i = 0; i < returnCodeArray.size(); i++) {
                codes.add(returnCodeArray.getLong(i));
            }
            if (codes.size() <= 0) {
                commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
                commonResult.setMessage("request params is null!");
                return JSON.toJSONString(commonResult);
            }

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");

            Map<String, List<OrderRoDetail>> data = new HashMap<>();
            for (Long returnId : codes) {
                data.put(returnId + "", roOrderService.getOrderRoDetailList(returnId));
            }

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(data);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage("系统繁忙，请稍后再试，或联系相关人员处理");
            return JSON.toJSONString(commonResult);
        }

    }

    @GET
    @Path("getRoHeadsByOrderIds")
    @Override
    public String getRoHeadsByOrderIds(@QueryParam("orderIds") String orderIds) {

        String logSign = ToolService.getLogSign();
        logger.info("getRoHeadsByOrderIds Request logSign:" + logSign + ", Request Url:" + ToolService.getUrl() + "，" + orderIds);
        CommonResult commonResult = new CommonResult();
        if (StringUtils.isBlank(orderIds)) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("getRoHeadsByOrderIds params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            JSONArray orderCodeArray = JSONArray.parseArray(orderIds);
            List<Long> codes = new ArrayList<>();
            for (int i = 0; i < orderCodeArray.size(); i++) {
                codes.add(orderCodeArray.getLong(i));
            }
            if (codes.size() <= 0) {
                commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
                commonResult.setMessage("getRoHeadsByOrderIds params is null!");
                return JSON.toJSONString(commonResult);
            }

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");

            Map<String, List<OrderRoHead>> data = new HashMap<>();
            for (Long orderId : codes) {

                data.put(orderId + "", roOrderService.getRoHeadList(orderId));
            }

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(data);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage("系统繁忙，请稍后再试，或联系相关人员处理");
            return JSON.toJSONString(commonResult);
        }

    }

    @GET
    @Path("getOrderCountNowByAddress")
    @Override
    public String getOrderHeadCountNowByAddressCode(@QueryParam("addressCode") Long addressCode) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (addressCode == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            Integer count = this.findOrderByMisService.getOrderHeadCountNowByAddressCode(addressCode);
            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(count);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getNofinishOrderCountNowByUserCode")
    @Override
    public String getNofinishOrderCountNowByUserCode(@QueryParam("userCode") Long userCode) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (userCode == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }
        try {
            Integer count = this.findOrderByMisService.getNofinishOrderCountNowByUserCode(userCode);
            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(count);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getMarketCountByAddress")
    @Override
    public String getOrderHeadCountForMarketByAddressCode(@QueryParam("addressCode") Long addressCode) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (addressCode == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            Map<String, Object> resultMap = this.findOrderByMisService.getOrderHeadCountAndTimeByAddressCode(addressCode);
            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(resultMap);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getOrderForAfterSales")
    @Override
    public String getOrderForAfterSales(@QueryParam("addressCode") Long addressCode) {
        logger.info("Request logSign : " + ToolService.getLogSign());
        CommonResult commonResult = new CommonResult();
        if (addressCode == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            MisOrderHeadForList misOrderHeadForList = findOrderByMisService.getOrderForAfterSales(addressCode);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(misOrderHeadForList);
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    @GET
    @Path("getReceiptInfoByOrderIds")
    @Override
    public String getReceiptInfoByOrderIds(@QueryParam("orderIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RECEIPT_GET_INFO);
    }

    @GET
    @Path("getDeliveryInfoByOrderIds")
    @Override
    public String getDeliveryInfoByOrderIds(@QueryParam("orderIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.DELIVERY_GET_INFO);
    }

    @GET
    @Path("getReceiptByDeliveryId")
    @Override
    public String getReceiptByDeliveryId(@QueryParam("shipping_order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.RECEIPT_BY_DELIVERY_ID);
    }

    @GET
    @Path("getReturnByDeliveryId")
    @Override
    public String getReturnByDeliveryId(@QueryParam("shipping_order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.RETURN_BY_DELIVERY_ID);
    }

    @GET
    @Path("getDeliveryDetails")
    @Override
    public String getDeliveryDetails(@QueryParam("shippingIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.DELIVERY_GET_DETAIL);
    }

    @GET
    @Path("getReceiptInfoByDeliveryIds")
    @Override
    public String getReceiptInfoByDeliveryIds(@QueryParam("shippingIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.DELIVERY_GET_RECEIPT_INFO);
    }

    @POST
    @Path("getReceiptInfoByDeliveryIds4tms")
    @Override
    public String getReceiptInfoByDeliveryIds4Tms(QuerySignTmsDto  querySignTmsDto) {

        String codes = querySignTmsDto.getShippingIds().toJSONString();
        return this.getOrders(codes, InterfaceEnum.DELIVERY_GET_RECEIPT_INFO);
    }

    @GET
    @Path("getRelateOrders")
    @Override
    public String getRelateOrders(@QueryParam("order_id") Long code) {
        return this.getOrder(code, InterfaceEnum.RELATE_ORDERS);
    }

    @GET
    @Path("getReturnHeadsByOrderIds")
    @Override
    public String getReturnHeadsByOrderIds(@QueryParam("orderIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RETURN_HEAD_ORDER);
    }

    @GET
    @Path("getReturnDetailsByOrderIds")
    @Override
    public String getReturnDetailsByOrderIds(@QueryParam("orderIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RETURN_DETAIL_ORDER);
    }

    @GET
    @Path("getReturnHeadsByDeliveryIds")
    @Override
    public String getReturnHeadsByDeliveryIds(@QueryParam("shippingIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RETURN_HEAD_DELIVERY);
    }

    @GET
    @Path("getReturnDetailsByDeliveryIds")
    @Override
    public String getReturnDetailsByDeliveryIds(@QueryParam("shippingIds") String codes) {
        return this.getOrders(codes, InterfaceEnum.RETURN_DETAIL_DELIVERY);
    }

    @GET
    @Path("getReceiptDetails")
    @Override
    public String getReceiptDetails(@QueryParam("receipt_order_id") Long code, @QueryParam("query") String query) {
        return this.getOrder(code, query, InterfaceEnum.RECEIPT_DETAILS);
    }

    @GET
    @Path("getReceiptDetailInfo")
    @Override
    public String getReceiptDetailInfo(@QueryParam("receipt_order_id") Long code, @QueryParam("receipt_detail_id") Long detailCode) {
        return this.getOrder(code, detailCode, InterfaceEnum.RECEIPT_DETAIL_INFO);
    }

    @Override
    @GET
    @Path("getOrderList")
    public String getOrderList(@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime) {
        CommonResult commonResult = new CommonResult();
        if (org.apache.commons.lang3.StringUtils.isAnyBlank(startTime, endTime)) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            JSONArray skuQtys = findOrderByMisService.getOrderHeadListByTimes(Integer.parseInt(startTime), Integer.parseInt(endTime));

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            commonResult.setData(skuQtys);

            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    private String getOrders(String codeString, InterfaceEnum interfaceEnum) {
        long startTime = System.currentTimeMillis();
        String logSign = ToolService.getLogSign();
        logger.info("Request logSign:" + logSign + ", Request Url:" + ToolService.getUrl());
        CommonResult commonResult = new CommonResult();
        if (StringUtils.isBlank(codeString)) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        Long venderId = this.getVenderId();
        try {
            JSONArray orderCodeArray = JSONArray.parseArray(codeString);
            List<Long> codes = new ArrayList<>();
            for (int i = 0; i < orderCodeArray.size(); i++) {
                codes.add(orderCodeArray.getLong(i));
            }
            if (codes.size() <= 0) {
                commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
                commonResult.setMessage("request params is null!");
                return JSON.toJSONString(commonResult);
            }

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success.");
            if (interfaceEnum == InterfaceEnum.ORDER_LIST) {
                List<MisOrderHeadForList> misOrderHeadForListList = findOrderByMisService.getOrderHeadList(codes);

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("orderHeadList", misOrderHeadForListList);

                commonResult.setData(jsonObject);
            } else {
                Object orders = null;
                switch (interfaceEnum.getIndex()) {
                    case 3:
                        orders = findOrderByMisService.getDeliveryOrderList(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 4:
                        orders = findOrderByMisService.getReceiptOrderList(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 5:
                        orders = findOrderByMisService.getReturnOrderList(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 9:
                        orders = findOrderByMisService.getReceiptInfoByOrderIds(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 10:
                        orders = findOrderByMisService.getDeliveryInfoByOrderIds(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 13:
                        orders = findOrderByMisService.getDeliveryDetails(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 14:
                        orders = findOrderByMisService.getReceiptInfoByDeliveryIds(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 16:
                        orders = findOrderByMisService.getReturnHeadsByOrderIds(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 17:
                        orders = findOrderByMisService.getReturnDetailsByOrderIds(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 18:
                        orders = findOrderByMisService.getReturnHeadsByDeliveryIds(codes, interfaceEnum, logSign, venderId);
                        break;
                    case 19:
                        orders = findOrderByMisService.getReturnDetailsByDeliveryIds(codes, interfaceEnum, logSign, venderId);
                        break;
                }

                commonResult.setData(orders);
            }

            logger.info("This request cost time is " + (System.currentTimeMillis() - startTime) + "ms");
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

    private String getOrder(Long code, InterfaceEnum interfaceEnum) {
        return this.loadOrder(code, null, interfaceEnum);
    }

    private <T> String getOrder(Long code, T query, InterfaceEnum interfaceEnum) {
        return this.loadOrder(code, query, interfaceEnum);
    }

    private <T> String loadOrder(Long code, T query, InterfaceEnum interfaceEnum) {
        long startTime = System.currentTimeMillis();
        String logSign = ToolService.getLogSign();
        logger.info("Request logSign:" + logSign + ", Request Url:" + ToolService.getUrl());
        CommonResult commonResult = new CommonResult();
        if (code == null) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        Long venderId = this.getVenderId();

        try {
            if (interfaceEnum == InterfaceEnum.ORDER_DETAIL) {
                MisOrderHeadForDetail misOrderHeadForDetail = findOrderByMisService.getOrderHead(code);

                if (misOrderHeadForDetail == null) {
                    commonResult.setCode(ReturnCode.ORDER_IS_NOT_EXIST);
                    commonResult.setMessage("order is not found by orderCode!");
                    return JSON.toJSONString(commonResult);
                }

                JSONObject orderObject = new JSONObject();
                orderObject.put("orderHead", misOrderHeadForDetail);
                orderObject.put("orderDetail", misOrderHeadForDetail.getMisOrderDetailForDetailList());

                JSONArray orderArray = new JSONArray();
                orderArray.add(orderObject);

                JSONObject returnDataObject = new JSONObject();
                MisOrderHeadForDetail misOrderHeadForDetail1New = new MisOrderHeadForDetail();
                BeanUtils.copyProperties(misOrderHeadForDetail, misOrderHeadForDetail1New);
                returnDataObject.put("orderInfo", misOrderHeadForDetail1New);
                returnDataObject.put("orderList", orderArray);

                commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
                commonResult.setMessage("success.");
                commonResult.setData(returnDataObject);
            } else {
                Object order = null;
                switch (interfaceEnum.getIndex()) {
                    case 6:
                        order = findOrderByMisService.getDeliveryOrder(code, interfaceEnum, logSign, venderId);
                        break;
                    case 7:
                        order = findOrderByMisService.getReceiptOrder(code, interfaceEnum, logSign, venderId);
                        break;
                    case 8:
                        order = findOrderByMisService.getReturnOrder(code, interfaceEnum, logSign, venderId);
                        break;
                    case 11:
                        order = findOrderByMisService.getReceiptByDeliveryId(code, interfaceEnum, logSign, venderId);
                        break;
                    case 12:
                        order = findOrderByMisService.getReturnByDeliveryId(code, interfaceEnum, logSign, venderId);
                        break;
                    case 15:
                        order = findOrderByMisService.getRelateOrders(code, interfaceEnum, logSign, venderId);
                        break;
                    case 20:
                        order = findOrderByMisService.getReceiptDetails(code, String.valueOf(query), logSign, venderId);
                        break;
                    case 21:
                        order = findOrderByMisService.getReceiptDetailInfo(code, Long.valueOf(String.valueOf(query)), logSign, venderId);
                        break;
                    case 22:
                        order = findOrderByMisService.getAfterSaleReturnOrder(code, interfaceEnum, logSign, venderId);
                        break;
                    case 23:
                        order = findOrderByMisService.getReceiptOrder2(code, interfaceEnum, logSign, venderId);
                        break;
                }
                //TODO:装配
                commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
                commonResult.setMessage("success.");
                commonResult.setData(order);
            }

            logger.info("This request cost time is " + (System.currentTimeMillis() - startTime) + "ms");
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            logger.info("This request cost time is " + (System.currentTimeMillis() - startTime) + "ms");
            return JSON.toJSONString(commonResult);
        }
    }

}
